package com.it.iostream.Service.Impl;

import com.it.iostream.Dao.ResPonseResult;
import com.it.iostream.Utils.FTPUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;

import static com.it.iostream.Utils.ResPonseResultUtils.ResResult;

@Service
public class FtpServiceImpl extends BaseService implements com.it.iostream.Service.FtpService {

 static  HashMap<String,String> hm = new HashMap<>();

    @Override
    public ResPonseResult uploadfile(String ipAddress,String userName,String password,String linuxsrc, String file) throws Exception {
        try {
            // 1、首先远程连接ssh
            FTPUtils.getInstance().loginftp(ipAddress, userName, password);
            logger.info("连接Linux,ip地址为" + ipAddress + ",账号是" + userName + ",连接成功.....");
            //上传图片
            FTPUtils.getInstance().uploadFile(linuxsrc, file);
            //关闭链接
            FTPUtils.getInstance().closeSession();
        } catch (Exception e) {
            e.printStackTrace();
            return  ResResult(404,"失败",hm);
        }

        return  ResResult(200,"成功",hm);

    }

    @Override
    public ResPonseResult downloadfile(String ipAddress, String userName, String password, String linuxsrc, String localaddr) throws Exception{
        try {
            // 1、首先远程连接ssh
            FTPUtils.getInstance().loginftp(ipAddress, userName, password);
            logger.info("连接Linux,ip地址为" + ipAddress + ",账号是" + userName + ",连接成功.....");
            //上传图片
            FTPUtils.getInstance().fileDownload(linuxsrc, localaddr);
            //关闭链接
            FTPUtils.getInstance().closeSession();
        } catch (Exception e) {
            e.printStackTrace();
            return  ResResult(404,"失败",hm);
        }
        return  ResResult(200,"成功",hm);

    }

    @Override
    public ResPonseResult deletefile(String ipAddress, String userName, String password, String linuxsrc) throws Exception{
        try {
            // 1、首先远程连接ssh
            FTPUtils.getInstance().loginftp(ipAddress, userName, password);
            logger.info("连接Linux,ip地址为" + ipAddress + ",账号是" + userName + ",连接成功.....");
            //上传图片
            FTPUtils.getInstance().deleteFile(linuxsrc);
            //关闭链接
            // FTPUtils.getInstance().closeSession();
        } catch (Exception e) {
            e.printStackTrace();
            return  ResResult(404,"失败",hm);
        }
        return  ResResult(200,"成功",hm);
    }
}
